using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Microsoft.Devices.Sensors;
using System.IO;
using System.IO.IsolatedStorage;

namespace IcyTowerMobile
{
	/// <summary>
	/// This is the main type for your game
	/// </summary>
	public class Game1 : Microsoft.Xna.Framework.Game
	{
		#region Fields

		GraphicsDeviceManager graphics;
		ScreenManager screenManager;

		#endregion

		#region Initialization

		/// <summary>
		/// The main game constructor.
		/// </summary>
		public Game1()
		{
			Content.RootDirectory = "Content";

			graphics = new GraphicsDeviceManager(this);
			graphics.IsFullScreen = true;
			TargetElapsedTime = TimeSpan.FromTicks(333333);

			// you can choose whether you want a landscape or portait
			// game by using one of the two helper functions.
			InitializePortraitGraphics();
			// InitializeLandscapeGraphics();

			// Create the screen manager component.
			screenManager = new ScreenManager(this);

			Components.Add(screenManager);

			// attempt to deserialize the screen manager from disk. if that
			// fails, we add our default screens.
			if (!screenManager.DeserializeState())
			{
				// Activate the first screens.
				screenManager.AddScreen(new BackgroundScreen(), null);
				screenManager.AddScreen(new MainMenuScreen(), null);
			}

			InitializeHighscore();
		}

		protected override void OnExiting(object sender, System.EventArgs args)
		{
			// serialize the screen manager whenever the game exits
			screenManager.SerializeState();

			base.OnExiting(sender, args);
		}

		/// <summary>
		/// Helper method to the initialize the game to be a portrait game.
		/// </summary>
		private void InitializePortraitGraphics()
		{
			graphics.PreferredBackBufferWidth = 480;
			graphics.PreferredBackBufferHeight = 800;
		}

		/// <summary>
		/// Helper method to initialize the game to be a landscape game.
		/// </summary>
		private void InitializeLandscapeGraphics()
		{
			graphics.PreferredBackBufferWidth = 800;
			graphics.PreferredBackBufferHeight = 480;
		}

		/// <summary>
		/// Initializes the highscore data file.
		/// </summary>
		private void InitializeHighscore()
		{
			IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();

			// check to see if the save exists
			if (!storage.FileExists(IsolatedStorageManager.FILENAME_HIGHSCORES))
			{
				// if the file doesn't exist, make a fake one
				IsolatedStorageFileStream stream = storage.CreateFile(IsolatedStorageManager.FILENAME_HIGHSCORES);
				stream.Close();

				// create the data to save
				HighscoreData data = new HighscoreData(5);

				// save empty highscore struct
				IsolatedStorageManager.GetInstance().SaveHighScores(data);
			}
		}

		#endregion

		#region Draw

		/// <summary>
		/// This is called when the game should draw itself.
		/// </summary>
		protected override void Draw(GameTime gameTime)
		{
			graphics.GraphicsDevice.Clear(Color.Black);

			// The real drawing happens inside the screen manager component.
			base.Draw(gameTime);
		}

		#endregion
	}

	#region Entry Point

	/// <summary>
	/// The main entry point for the application.
	/// </summary>
	static class Program
	{
		static void Main()
		{
			using (Game1 game = new Game1())
			{
				game.Run();
			}
		}
	}

	#endregion
}
